package com.example.patentback.controller;

import cn.hutool.core.map.MapUtil;
import com.example.patentback.entity.SysMenu;
import com.example.patentback.entity.SysMenuDto;
import com.example.patentback.entity.SysUser;
import com.example.patentback.service.SysMenuService;
import com.example.patentback.service.SysRoleMenuService;
import com.example.patentback.service.SysUserService;
import com.example.patentback.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    SysRoleMenuService sysRoleMenuService;

    //Principal代表当前认证了的用户
    @GetMapping("/nav")
    public Result nav(Principal principal) {
        SysUser sysUser = sysUserService.getByUsername(principal.getName());

        // 获取权限信息
        String authorityInfo = sysUserService.getUserAuthorityInfo(sysUser.getId());// ROLE_admin,ROLE_normal,sys:user:list,....
        String[] authorityInfoArray = StringUtils.tokenizeToStringArray(authorityInfo, ",");

        // 获取导航栏信息
        List<SysMenuDto> navs = sysMenuService.getCurrentUserNav();

        return Result.success(MapUtil.builder()
                .put("authoritys", authorityInfoArray)
                .put("nav", navs)
                .map()
        );
    }

    @GetMapping("/menuInfo/{id}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result menuInfo(@PathVariable(name = "id") Long id) {
        return Result.success(sysMenuService.getMenuById(id));
    }

    @GetMapping("/allMenus")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result allMenus() {
        List<SysMenu> allMenus = sysMenuService.getAllMenus();
        return Result.success(allMenus);
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result saveMenu(@Validated @RequestBody SysMenu sysMenu) {
        int i = sysMenuService.saveMenu(sysMenu);
        return Result.success(sysMenu);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result updateMenu(@Validated @RequestBody SysMenu sysMenu) {
        int i = sysMenuService.updateMenu(sysMenu);
        //菜单变更，清除权限缓存
        sysUserService.clearUserAuthorityInfoByMenuId(sysMenu.getId());
        return Result.success(sysMenu);
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result deleteMenu(@PathVariable(name = "id") Long id) {
        int i = sysMenuService.deleteMenu(id);
        if (i == 0) {
            return Result.error("201", "请先删除子菜单");
        }
        sysUserService.clearUserAuthorityInfoByMenuId(id);
        //同步删除中间关联表
        sysRoleMenuService.deleteRoleMenuByMenuId(id);
        return Result.success("删除成功");
    }
}
